import string
import sys
import pickle
import urllib2
from document import *
from word import *

STORESPACE = 9
WORDTAG = 'newword'

def read(file_name):
    """
    Read a file address, output its content as a string, and 
    close the file when done. 
    
    Return the content of the file in a string
    """
    try:
        input_file=open(file_name, 'r')
    except:
        try:
            input_file = urllib2.urlopen(file_name)
        except:
            return ''

    input_string = input_file.read()
    input_file.close()
    return input_string

def write(file_name, text):
    output_file = open(file_name, 'w')
    output_file.write(text)
    output_file.close()

def storeIndex(doclist, file):
    """
    We pickle the content from file_address
    and dump it into doc_list
    """
    indexf = open(file, 'w')
    for doc in doclist:
        writeItem(indexf, doc.location) #0
        writeItem(indexf, doc.name)     #1
        writeItem(indexf, doc.type)     #2
        writeItem(indexf, doc.doclength)    #3
        writeItem(indexf, doc.input_title)  #4
        writeItem(indexf, doc.input_authors) #5
        writeList(indexf, doc.title)    #6
        writeList(indexf, doc.authors) #7
        writeWordDic(indexf, doc.word_objects) #8
    indexf.close()

def writeItem(output, item):
    output.write(str(item) + '\n')

def writeList(output, list):
    for item in list:
        output.write(str(item) + ' ')
    output.write('\n')

def writeWordDic(output, dic):
    for string in dic.keys():
        output.write(WORDTAG + ' ' + str(string) + ' ')
        for int in dic[string].locations:
            output.write(str(int) + ' ')
    output.write('\n')
    
def openIndex(file_address):
    """
    Return a list of all the indexed documents. 
    """
    doc_list = []
    inputf = open(file_address, 'r')
    lines = inputf.read().split('\n')
    readfuncts = {0:readLoc, 1:readName, 2:readType, 3:readLen, 4:readInTitle, 5:readInAuth, 6:readTitle, 7:readAuth, 8:readWords}
    for int in range(len(lines)):
        if (int % STORESPACE == 0):
            doc = document()
        readfuncts[int % STORESPACE](lines[int], doc)
        if (int % STORESPACE == STORESPACE-1):
            doc_list.append(doc)
    inputf.close()
    return doc_list

def readLoc(line, doc):
    doc.location = line
    
def readName(line, doc):
    doc.name = line

def readType(line, doc):
    doc.type = line
    
def readLen(line,doc):
    doc.doclength = int(line)

def readInTitle(line, doc):
    doc.input_title = line
    
def readInAuth(line, doc):
    doc.input_authors = line

def readTitle(line, doc):
    doc.title = line.split()

def readAuth(line, doc):
    doc.authors = line.split()

def readWords(line, doc):
    previous = ''
    currentword = ''
    worddic = {}
    line = line.split()
    for string in line:
        if (previous == WORDTAG):
            currentword = string
            worddic[string] = word(string)       
        elif(currentword != '' and isInt(string)):
            worddic[currentword].updateWord(int(string))
        previous = string
    doc.word_objects = worddic  
    
def isInt(string):
    isint = True
    try:
        num = int(string)
    except ValueError:
        isint = False
    return isint
